package org.example;


import java.sql.*;
import java.text.MessageFormat;
import java.util.Scanner;

public class Demo01_DriverManager {

    public static void main(String[] args) {
        Connection connection = null;
        Statement statement = null;
        ResultSet resultSet = null;

        try {
            // 1. 加载数据库厂商提供的的驱动
            Class.forName("com.mysql.cj.jdbc.Driver");
            // 2.获取数据库连接
            connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/test?characterEncoding=utf8" +
                    "&allowPublicKeyRetrieval=true&useSSL=false", "root", "12345678");
            //3.创建statement对象
            statement = connection.createStatement();
            // 4. 定义sql并执行sql语句
            System.out.println("请输入学生姓名:");
            Scanner scanner = new Scanner(System.in);
            // 接受用户的输入
            String name = scanner.next();
            String sql = "select student_id, sn,name,mail,class_id from student where name = '"+name+"'";
            // 5. 执行SQL,获取查询结果
            //执行select语句
            resultSet = statement.executeQuery(sql);
            //执行update delete insert 语句
            //statement.executeUpdate(sql);
            // 6.对结果集进行遍历,获取数据
            // 如果下一条有记录返回true,没有则返回false
            while(resultSet.next()){
                // 获取学生ID
                long stuId = resultSet.getLong(1);
                String stuSn = resultSet.getString(2);
                String stuName = resultSet.getString(3);
                String mail = resultSet.getString(4);
                long classId = resultSet.getLong(5);
                System.out.println(MessageFormat.format("学生编号 = {0}, 学号 = {1},学生姓名 = {2}, " +
                                "学生邮箱 = {3}, 班级编号 = {4}",
                        stuId,stuSn, stuName,mail,classId));
            }
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            // 依次释放资源,关闭连接
            if (resultSet != null){
                try {
                    resultSet.close();
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }
            if (statement != null){
                try {
                    statement.close();
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }
            if(connection != null){
                try {
                    connection.close();
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }
        }

    }
}


